#include<iostream>

using namespace std;

const int N = 1e6 + 10;

int length(int q[])
{
    return (q[N - 1] - q[N - 2] + N - 1) % (N - 1);
}

bool empty(int q[])
{
    return q[N - 1] == q[N - 2];
}

bool full(int q[])
{
    return (q[N - 1] - q[N - 2] + 1) % (N - 1) == 0;
}

void push(int q[], int x)
{
    if (full(q))
        return;
    q[q[N - 1]] = x;
    q[N - 1] = (q[N - 1] + 1) % (N - 1);
}

int pop(int q[])
{
    if (empty(q))
        return ~(1 << 31);
    int tmp = q[q[N - 2]];
    q[N - 2] = (q[N - 2] + 1) % (N - 1);
    return tmp;
}

int query(int q[])
{
    if (empty(q))
        return ~(1 << 31);
    return q[q[N - 2]];
}

int q[N], a[N], b[N];
int n, k;

int main(void)
{
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++)
        scanf("%d", &q[i]);
    int l, r, max, min;
    for (l = 0, r = l + k - 1; r < n; l++, r++)
    {
        max = min = q[l];
        for (int i = l + 1; i <= r; i++)
        {
            if (max < q[i])
                max = q[i];
            if (min > q[i])
                min = q[i];
        }
        push(a, min);
        push(b, max);
    }
    while (!empty(a))
        printf("%d ", pop(a));
    printf("\n");
    while (!empty(b))
        printf("%d ", pop(b));
}